/**
 * Created by acer on 2015/7/21.
 */
var upload='/file/upload',
    findOrderByConditionsUrl='/order/findOrderByConditions'
    findOrderInfoByOrderIDUrl='/order/findOrderInfoByOrderID?',
        deleteOrderByBatchIDUrl='/order/deleteOrderByBatchID?batchID=',
    doubleCheckedUrl='/order/doubleChecked?batchID=',
    batchIDserachList=request.QueryString('batchID');

function pageInnerConfig(){
    this.startAt=0
        this.pageSize=10000,
        this.maxPage=null,
        this.currentPage=null,
        this.isChangePage=false,
        this.searchAllCount=false,
        this.currentId=null,
        this.searchData={},
        this.toPage=1,
            this.compareContent=null
    return this
}

$(document).ready(function(){
    if(batchIDserachList){
        $('.originalTypeNext').show()
        $('#ensureSubmitList').bind('click',function(){
            var param={
                url:appSocket+doubleCheckedUrl+batchIDserachList,
                data:{},
                method:'get'
            }
            ajaxRequest(param,function(data){
             if(data.status==200){
                 alert(data.record)
                 window.close()
             }
                else{
                 alert('修改失败，请重试')
             }
            })
        })
        $('#ensureDeleteList').bind('click',function(){
            if(confirm('确认删除？')){
                var param={
                    url:appSocket+deleteOrderByBatchIDUrl+batchIDserachList,
                    data:{},
                    method:'get'
                }
                ajaxRequest(param,function(data){
                    ats('删除成功')
                    window.close()
                });
            }
        })
        commonConfig={pageSize:100000000,startAt:0,searchData:{batchID:batchIDserachList},compare:null}
    }
    else{
        $('.originalType').show()
        commonConfig=new pageInnerConfig()
    }
    judgeSearch(commonConfig)
})

function clean(){
    var modal=$('#modal-listDetailModal')
    modal.modal('show')
    modal.find('input:not(input:radio)').val('')
    modal.find('input:radio').prop('checked',false)
}
var Mymodel={}

Mymodel.GetMessageList=ko.observableArray([]);
Mymodel.GetPaging=ko.observableArray([]);


Mymodel.ModelDataArray=ko.observableArray([]);

Mymodel.changePage = function(page){
    var jumpPage
    jumpPage=page=parseInt(page)
    if(!commonConfig.isChangePage){
        commonConfig.isChangePage=true;
        commonConfig.startAt=page;
        judgeSearch(commonConfig)
    }
};

function getBidsListCallback(data){
    $('#showList >thead >tr th:first-child').find('input').prop('checked',false)
    isChangePage=false;
    for(var i in data.data.rows){
        if(data.data.rows[i].status==0){
            data.data.rows[i].status='启用'
        }
        else{
            data.data.rows[i].status='禁用'
        }
    }
    Mymodel.GetMessageList(data.data.rows)
    var pages=paging(data.data.pages)
    $('.marginTopP').html('')
    Mymodel.GetPaging(pages.pages)
}

function sendCreateInstructionCallback(data){
    if(data.data.success==false){
        alert(data.data.message)
        return false
    }
    $('#modal-addBackEnd').modal('hide')
    toPage=1
    searchAllCount=0
    var data = {
        "startKey":"",
        "lastKey":'',
        "jumpPage":0,
        "limit":pageSize
    }
    getBidsList(data,getBidsListCallback)
}

function checkIt(k){
    $('#listDetailModalSaveBtn').unbind('click',updateIt).bind('click',updateIt)
    commonConfig.currentId= k.id
    clean()
    var data={
        orderID: k.id
    }
    getAjax(findOrderInfoByOrderIDUrl,data,function(data){
        commonConfig.compareContent=data.record
     for(var i in commonConfig.compareContent){
         $('#'+i).val(commonConfig.compareContent[i])
     }
    })
}

function sendCheckedBidCallback(data){
    for(var i in data.data[0]){
        $('#'+i+'Modify').val(data.data[0][i])
        if(i=='status'){
            if(data.data[0][i]==0){
                $('#0').prop('checked',true)
            }
            else{
                $('#1').prop('checked',true)
            }
        }
    }
}


function updateIt(){
    //if(alertEmpty('#modal-listDetailModal')){
    //
    //}
    for(var i in commonConfig.compareContent){
            commonConfig.compareContent[i]=$('#'+i).val()
    }
    commonConfig.compareContent['_id']=commonConfig.currentId
    postAjax(commmonUrlArray.updateOrderUrl,commonConfig.compareContent,function(data){
        if(data.status==200){
            $('#modal-listDetailModal').modal('hide')
            judgeSearch(commonConfig)
        }
    })
}


function searchList(){
    var param={}
    $('.searchGroupInput').each(function(e){
        if(isNull($(this).find('input').val())==false){
            param[$(this).find('input').attr('id')]=$(this).find('input').val()
        }
    })
    commonConfig.searchData=param
    if(batchIDserachList){
        commonConfig.searchData['batchID']=batchIDserachList
    }
    judgeSearch(commonConfig)
}


function pageUp(){
    if(commonConfig.currentPage<=1){
        alert('没有上一页了！');
    }else if(!commonConfig.isChangePage){
        commonConfig.toPage=commonConfig.currentPage-1;
        commonConfig.isChangePage=true;
        judgeSearch(commonConfig)
    }
}

function pageDown(){
    if(commonConfig.currentPage>=commonConfig.maxPage){
        alert('没有下一页了！');
    }else if(!commonConfig.isChangePage){
        commonConfig.toPage=commonConfig.currentPage+1;
        commonConfig.isChangePage=true;
        judgeSearch(commonConfig)
    }
}



function judgeSearch(config){
    var params = {
        url:appSocket+findOrderByConditionsUrl+'?pageSize='+config.pageSize+'&&startAt='+config.startAt+'&&'+decodeURIComponent($.param(config.searchData)).replace(/'/g,''),
        data:{},
        method:'get'
    }
    ajaxRequest(params,function(data){
        for(var i in data.recordset){
            //if(data.recordset[i].FORDERSTATUS==undefined){
            //    data.recordset[i].FORDERSTATUS=''
            //}
            try{data.recordset[i].FLEASEBEGINTIME=new Date(data.recordset[i].FLEASEBEGINTIME).Format('yyyy-mm-dd hh:ii')}
            catch(err){

            }
            try{data.recordset[i].FLEASEENDTIME=new Date(data.recordset[i].FLEASEENDTIME).Format('yyyy-mm-dd hh:ii')}
            catch(err){

            }
            try{data.recordset[i].FTAKEAWAYTIME=new Date(data.recordset[i].FTAKEAWAYTIME).Format('yyyy-mm-dd hh:ii')}
            catch(err){

            }
        }
        Mymodel.GetMessageList(data.recordset)
        config.maxPage=data.data.pages;
        pageNumberFullfill(config)
    })
}

deleteIt=function(k){
    var params={
        orderID: k.id
    }
    if(confirm('是否删除')){
        getAjax(commmonUrlArray.deleteOrderUrl,params,function(data){
            if(data.status==200){
                judgeSearch(commonConfig)
            }
        })
    }
}

resetRan=function(){
    $('.searchGroupInput').each(function(e){
        $(this).find('input').val('')
    })
    searchList()
}

function pageNumberFullfill(config){
    var pagesHead='<li  onclick="pageUp()"><a aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'
    var pagesTail='<li onclick="pageDown()"><a aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'
    $('.marginTopP').append(pagesTail)
    $('.marginTopP').prepend(pagesHead)
    var active = "#toPage_"+config.toPage;
    $(active).addClass('active');
    config.currentPage=config.toPage;
    config.isChangePage=false;
}

exportIt=function(){
    window.open(appSocket+'/order/exportExcel')
}

checkItAccurate=function(it){
     currentId=it.id;

}

ko.applyBindings(Mymodel);